package cn.iocoder.common.utils.common.util;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.Type;
import org.springframework.beans.BeanUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ObjectUtils {
    private static MapperFacade mapper;

             static {
                 MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
                 mapper = mapperFactory.getMapperFacade();
             }
    /***
     * 业务逻辑判断
     * 前提条件为 入参都为空 也是不相等
     * */
    public static boolean StringsIsEquals(String s1,String s2){
        if (s1==null||s2==null){
            return false;
        }
        return s1.equals(s2);
    }

    public static <S, D> List<D> mapList(List<S> sourceList, Type<S> sourceType, Type<D> destinationType) {

        return mapper.mapAsList(sourceList, sourceType, destinationType);
     }

     public static <S,D> List<D> copyList(Iterable<S> list, Class<D> dType){
        if(list==null){
            return Collections.emptyList();
        }
       return mapper.mapAsList(list,dType);
     }

     public static  <D> D copyObject(Object o, Class<D> dClass){
        if (o==null){
            return null;
        }
         D temp= null;
         try {
             temp = dClass.newInstance();
         } catch (InstantiationException e) {
            return null;
         } catch (IllegalAccessException e) {
            return null;
         }
         BeanUtils.copyProperties(o,temp);
        return  temp;
     }


     public static boolean isNull(Object... o){
        if (o.length==0){
            return true;
        }
        for ( Object o1:o){
            if (o1==null){
                return true;
            }
        }
        return false;
     }

}
